﻿using System;

namespace MandelbrotLib
{
    public class ComplexNumber
    {
        protected RealNumber mReal;
        protected RealNumber mImag;

        protected static ComplexNumber sZero
            = new ComplexNumber(RealNumber.Zero, RealNumber.Zero);

        public ComplexNumber()
        {
            mReal = RealNumber.Zero;
            mImag = RealNumber.Zero;
        }

        public static ComplexNumber Zero
        {
            get
            {
                return sZero;
            }
        }

        public RealNumber SquareLength
        {
            get
            {
                RealNumber sqrReal = this.mReal.Mul(this.mReal);
                RealNumber sqrImag = this.mImag.Mul(this.mImag);
                return sqrReal.Add(sqrImag);
            }
        }

        public ComplexNumber(RealNumber real, RealNumber imag)
        {
            mReal = real;
            mImag = imag;
        }

        public ComplexNumber Add(ComplexNumber that)
        {
            RealNumber real = this.mReal.Add(that.mReal);
            RealNumber imag = this.mImag.Add(that.mImag);

            return new ComplexNumber(real, imag);
        }

        public ComplexNumber Sub(ComplexNumber that)
        {
            RealNumber real = this.mReal.Sub(that.mReal);
            RealNumber imag = this.mImag.Sub(that.mImag);

            return new ComplexNumber(real, imag);
        }

        public ComplexNumber Mul(ComplexNumber that)
        {
            RealNumber real = this.mReal.Mul(that.mReal) 
                - this.mImag.Mul(that.mImag);
            RealNumber imag = this.mImag.Mul(that.mReal)
                + this.mReal.Mul(that.mImag);

            return new ComplexNumber(real, imag);
        }

        public ComplexNumber Div(int that)
        {
            RealNumber real = this.mReal.Div(that);
            RealNumber imag = this.mImag.Div(that);

            return new ComplexNumber(real, imag);
        }

        public ComplexNumber GetNegative()
        {
            RealNumber real = this.mReal.GetNegative();
            RealNumber imag = this.mImag.GetNegative();

            return new ComplexNumber(real, imag);
        }

        /*
         * 'decp' is the new positive decimal point position
         * only a value less than the original one is effective
         * which is the number of chunks after the decimal position
         */
        public void RoundTo(int decp)
        {
            mReal.RoundTo(decp);
            mImag.RoundTo(decp);
        }
    }
}
